package com.guigu.qingcheng.servicegoods.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.goods.TbAlbum;
import com.guigu.qingcheng.servicegoods.service.TbAlbumService;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/album")
public class TbAlbumController {

    @Autowired
    TbAlbumService albumService;

    //查询所有
    @GetMapping("findAll")
    public R findAll(){
        List<TbAlbum> list = albumService.list(null);
        if (list.size()>0)
            return R.ok().data("list",list);
        return R.error().data("list",list);
    }

    //分页不带条件
    @GetMapping("findPage")
    public R findPage(@RequestParam("page") int page, @RequestParam("size") @PathVariable int size){
        //创建page对象
        Page<TbAlbum> pageAlbum = new Page<>(page,size);
        albumService.page(pageAlbum,null);

        long total = pageAlbum.getTotal();//总记录数
        List<TbAlbum> records = pageAlbum.getRecords(); //数据list集合
        if (total>0 && records.size()>0)
            return R.ok().data("total",total).data("rows",records);
        return R.error();
    }

    //条件查询不带分页
    @PostMapping("findList")
    public R findList(@RequestBody Map<String,Object>searchMap){
        List<TbAlbum> list = albumService.findList(searchMap);
        if (list.size()>0){
            return R.ok().data("list",list);
        }
        return R.error().data("list",list);
    }

    //条件查询带分页
    @PostMapping("findPage")
    public R pageTeacherCondition(@RequestBody Map<String, Object> searchMap, @RequestParam("page") int page, @RequestParam("size") int size){
        Page<TbAlbum> pageAlbum = albumService.findPage(searchMap,page,size);
        if (pageAlbum.getRecords().size()>0){
            return R.ok().data("total",pageAlbum.getTotal()).data("rows",pageAlbum.getRecords());
        }
        return R.error().data("total",pageAlbum.getTotal()).data("rows",pageAlbum.getRecords());
    }

    //根据id查询
    @GetMapping("findById")
    public R findById(@RequestParam("id") int id){
        TbAlbum album = new TbAlbum();
        album = albumService.getById(id);
        return R.ok().data("album",album);
    }

    //增加数据
    @PostMapping("add")
    public R addAlbum(@RequestBody TbAlbum tbAlbum){
        Boolean flag = albumService.save(tbAlbum);
        if (flag)
            return R.ok();
        return R.error();
    }
    //修改数据
    @PostMapping("update")
    public R updateAlbum(@RequestBody TbAlbum tbAlbum){
        boolean flag = albumService.updateById(tbAlbum);
        if (flag)
            return R.ok();
        return R.error();
    }

    //删除数据
    @GetMapping("delete")
    public R deleteAlbumById(@RequestParam("id") Long id){
        boolean flag = albumService.removeById(id);
        if (flag)
            return R.ok();
        return R.error();
    }
}